package com.woniu.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.QueryTrainingDTO;
import com.woniu.entity.dto.SearchTrainingDTO;
import com.woniu.entity.vo.TrainingVO;
import com.woniu.service.ITrainingService;
import com.woniu.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@CrossOrigin //开放前端的跨域访问
@RestController
@RequestMapping("home")
@Api(tags = "首页+靶场演习")
@Slf4j
public class HomeController extends BaseController{
    @Autowired
    private ITrainingService trainingService;

    @Autowired
    private RedisTemplate redisTemplate;



    @GetMapping("/showFollow/{uid}")
    @ApiOperation("显示关注靶场信息")
    public Result showFollow(@PathVariable Integer uid ){
//        查询登录状态。。。
//        Object userInfo = session.getAttribute("userInfo");
//        固定userId，715，方便
//        userInfo="715";
//        if (userInfo!=null){
//          已登录，查询关注靶场信息
            List<TrainingVO> follows=trainingService.showFollow(uid);
            return toDataResult(follows);
//        }
//        else {
////            未登录
//            return toResult(false);
//        }
    }

    @GetMapping("/checkCollect")
    @ApiOperation("查询靶场是否被收藏")
    public Result checkCollect(@RequestParam Integer uid,@RequestParam String title){
        Boolean flag=trainingService.checkCollect(uid,title);
        return toDataResult(flag);
    }

    @GetMapping("/showRecommend")
    @ApiOperation("Redis展示推荐靶场信息")
//    从redis中取出定时任务中从数据库查询的推荐靶场信息
    public Result showRecommend(){
        Set recommendSet = redisTemplate.opsForSet().members("recommends");
//        如果redis中存在数据，直接返回
        if (recommendSet!=null && recommendSet.size() >0){
            return toDataResult(recommendSet);
        }
//        redis中不存在，主动查询数据库
        log.debug("主动查询数据库获取推荐靶场信息=====");
        Set<TrainingVO> recommendVOs=trainingService.recommend();
        //        把查询的数据存储到Redis中
        redisTemplate.opsForSet().add("recommends", recommendVOs.toArray());
        return toDataResult(recommendVOs);
    }

//    @GetMapping("/existKey")
//    @ApiOperation("判断Redis中key是否存在")
//    public boolean existKey(String key) {
//        return stringRedisTemplate.hasKey(key);
//    }

//    @GetMapping("/getRecommend")
//    @ApiOperation("数据库主动查询推荐靶场信息")
//    public Result getRecommend(){
//        log.debug("主动查询数据库获取推荐靶场信息=====");
//        Set<TrainingVO> recommends=trainingService.recommend();
//        //        把查询的数据存储到Redis中
//        Long save = redisTemplate.opsForSet().add("recommends", recommends.toArray());
//        Set result = redisTemplate.opsForSet().members("recommends");
//        System.out.println("result = " + result);
//        if (save>0){
//            log.debug("主动查询成功，已存储到Redis中=====");
//            return toDataResult(save);
//        }else
//            log.debug("主动查询失败=====");
//        return toDataResult(save);
//    }

    @GetMapping("/showParent")
    @ApiOperation("显示一级靶场名称")
    public Result showParent(){
        List<String> parentNames=new ArrayList<>();
        parentNames=trainingService.showParent();
        return toDataResult(parentNames);
    }

    @GetMapping("/getParentId/{pName}")
    @ApiOperation("一级靶场获取ParentId")
    public Result getParentId(@PathVariable String pName){
        Integer parentId = trainingService.getParentId(pName);
        return toDataResult(parentId);
    }

    @ApiOperation("搜索框")
    @PostMapping("/searchBar/{currentPage}/{pageSize}/{userId}/{title}")
    public Result searchBar(@PathVariable Integer currentPage,
                             @PathVariable Integer pageSize,
                            @PathVariable Integer userId,
                              @PathVariable String title,
                             @RequestBody SearchTrainingDTO searchTrainingDTO) {
        PageData pageData = trainingService.searchBar(currentPage, pageSize, userId,title, searchTrainingDTO);
        return toDataResult(pageData);
    }

    @ApiOperation("靶场演习跳转")
    @PostMapping("/trainingList/{currentPage}/{pageSize}/{userId}")
    public Result trainingList(@PathVariable Integer currentPage,
                            @PathVariable Integer pageSize,
                            @PathVariable Integer userId,
                            @RequestBody SearchTrainingDTO searchTrainingDTO) {
        PageData pageData = trainingService.searchBar(currentPage, pageSize, userId, null,searchTrainingDTO);
        return toDataResult(pageData);
    }


}
